package com.jz.control;

import com.jz.entity.Course;
import com.jz.entity.PageBean;
import com.jz.entity.ResultVo;
import com.jz.service.CourseService;
import com.jz.service.impl.CourseServiceImpl;
import com.jz.utils.DateUtils;
import com.jz.utils.FileUploadUtils;
import com.jz.utils.RespJsonUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.util.Map;
@WebServlet("*.cs")
@MultipartConfig

public class CourseServlet extends BaseServlet {
    //多态调用方法
     CourseService cs = new CourseServiceImpl();

    public void addCourse(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        //获取表单的数据
        Map<String, String[]> map = req.getParameterMap();
        //不能获取上传的图片和视频-->额外获取图片和视频
        Course c = new Course();
        //表单与对象之间的数据映射
        BeanUtils.populate(c,map);
        //单独给额外日期赋值(上架时间)
        c.setCreateTime(DateUtils.dateToStr());
        //
        boolean b = cs.addCourse(c);
        //
        ResultVo rv = null;
        if (b){
            //课程添加成功
            rv = new ResultVo(200, "课程上传成功", null);
        }else{
            rv = new ResultVo(500,"课程上传失败",null);
        };
        //解析json并响应
        RespJsonUtils.objectToJson(rv,resp);





    }
    //上传课程
    public void courseUpload(HttpServletRequest req,HttpServletResponse response)throws Exception{
        //获取保存文件信息的part
        Part part = req.getPart("file");
        //调用上传工具类,并且返回上传成功后返回的文件名字
        String filename = FileUploadUtils.upload(part, "f:/img");
        ResultVo rv = null;
        //判断上传的是图片还是视频
        if (filename.endsWith("png")||filename.endsWith("jpg")||filename.endsWith("gif")){
            //上传的图片
            rv = new ResultVo(201,"图片上传成功",filename);
        }else if (filename.endsWith("mp4")||filename.endsWith("avi")){
            //上传的视频
            rv = new ResultVo(202,"视频上传成功",filename);
        }else{
            //上传失败
            rv = new ResultVo(500,"上传失败",null);

        }
        //解析并相应
        RespJsonUtils.objectToJson(rv,response);
    }
    //分页查询
    public void  queryCourse(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        //获取前台传递的参数  当前页  curPage 页容量 pageSize  课程名模糊搜索内容  search
        String curPage = req.getParameter("curPage");
        String pageSize = req.getParameter("pageSize");
        String search = req.getParameter("search");
        //调用分页查询方法
        PageBean<Course> pb = cs.queryCourse(search, curPage, pageSize);
        RespJsonUtils.objectToJson(pb,resp);
    }
    //删除课程
    public void  delCourse(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        //获取要删除的课程id参数 cids
        String cids = req.getParameter("cids");
        boolean b = cs.deleCourse(cids);
        ResultVo rv = null;
        if(b){
            //删除成功
            rv = new ResultVo(200,"课程删除成功",null);
        }else{
            //删除失败
            rv = new ResultVo(500,"课程删除失败",null);
        }
        //解析成json并响应
        RespJsonUtils.objectToJson(rv,resp);


    }
    //删除课程
    public void deleteCourse(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        String cids = req.getParameter("cids");
        boolean b = cs.deleCourse(cids);
        ResultVo rv = null;
        if (b){
            rv = new ResultVo(200,"删除成功",null);
        }else{
          rv = new ResultVo(500,"删除失败",null);
        }
        RespJsonUtils.objectToJson(rv,resp);

    }

}
